package top.iridescent.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {
	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static String format(Date date) {
		return format(date, DATE_PATTERN);
	}

	public static String format(Date date, String pattern) {
		if(date != null){
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.format(date);
		}
		return null;
	}


	/**
	 * 根据传入的时间类型分别获取 今日:0,近7天:1,近1个月:2,近6个月:3,近12个月:4的 ***同比***起始时间 00:00
	 *
	 * @param dataType
	 * @return
	 */
	public static Date getTbStartTime(String dataType) {
		Calendar calendar = Calendar.getInstance();
		switch (dataType) {

			case "0":
				calendar.setTime(new Date());
				calendar.add(Calendar.YEAR, -1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				return calendar.getTime();
			case "1":
				calendar.setTime(new Date());
				calendar.add(Calendar.YEAR, -1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.add(Calendar.DAY_OF_MONTH, -6);
				return calendar.getTime();
			case "2":
				calendar.setTime(new Date());
				calendar.add(Calendar.YEAR, -1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.add(Calendar.MONTH, -1);
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				return calendar.getTime();
			case "3":
				calendar.setTime(new Date());
				calendar.add(Calendar.YEAR, -1);
				calendar.add(Calendar.MONTH, -5);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				return calendar.getTime();
			case "4":
				calendar.setTime(new Date());
				calendar.add(Calendar.YEAR, -1);
				calendar.add(Calendar.MONTH, -11);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				return calendar.getTime();
			default:
				return null;
		}
	}

	/**
	 * 获取同比结束时间,第二天凌晨00:00
	 *
	 * @return
	 */
	public static Date getTbEndTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}
	/**
	 * 根据传入的时间类型分别获取 今日:0,近7天:1,近1个月:2,近6个月:3,近12个月:4的 ***环比***起始时间 00:00
	 *
	 * @param dataType
	 * @return
	 */
	public static Date getHbStartTime(String dataType) {
		Calendar calendar = Calendar.getInstance();

		switch (dataType) {

			case "0":
				calendar.setTime(new Date());
				calendar.add(Calendar.DAY_OF_MONTH, -1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				return calendar.getTime();
			case "1":
				calendar.setTime(new Date());
				calendar.add(Calendar.DAY_OF_MONTH, -7);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.add(Calendar.DAY_OF_MONTH, -6);
				return calendar.getTime();
			case "2":
				calendar.setTime(new Date());
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.add(Calendar.MONTH, -2);
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				return calendar.getTime();
			case "3":
				calendar.setTime(new Date());
				calendar.add(Calendar.MONTH, -11);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				return calendar.getTime();
			case "4":
				calendar.setTime(new Date());
				calendar.add(Calendar.MONTH, -23);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				return calendar.getTime();
			default:
				return null;
		}
	}

	/**
	 * 根据传入的时间类型分别获取今日:0,近7天:1,近1个月:2,近6个月:3,近12个月:4的起始时间 00:00
	 *
	 * @param dataType
	 * @return
	 */
	public static Date getStartTime(String dataType) {
		Calendar calendar = Calendar.getInstance();

		switch (dataType) {

			case "0":
				calendar.setTime(new Date());
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				return calendar.getTime();
			case "1":
				calendar.setTime(new Date());
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.add(Calendar.DAY_OF_MONTH, -6);
				return calendar.getTime();
			case "2":
				calendar.setTime(new Date());
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.add(Calendar.MONTH, -1);
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				return calendar.getTime();
			case "3":
				calendar.setTime(new Date());
				calendar.add(Calendar.MONTH, -5);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				return calendar.getTime();
			case "4":
				calendar.setTime(new Date());
				calendar.add(Calendar.MONTH, -11);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				return calendar.getTime();
			default:
				return null;
		}
	}

	/**
	 * 获取结束时间,第二天凌晨00:00
	 *
	 * @return
	 */
	public static Date getEndTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}

	public static List<Date> getIntervalDate(String dataType) {
		List<Date> dateList = new ArrayList<>();
		Date startDate = getStartTime(dataType);
		Date endDate = getEndTime();
		Calendar calendar = Calendar.getInstance();
		switch (dataType) {

			case "0":
				dateList = getDateIntervalList(startDate, endDate, Calendar.MINUTE, 120);
				return dateList;
			case "1":
			case "2":
				dateList = getDateIntervalList(startDate, endDate, Calendar.DAY_OF_MONTH, 1);
				return dateList;
			case "3":
			case "4":
				dateList = getDateIntervalList(startDate, endDate, Calendar.MONTH, 1);
			default:
				return dateList;
		}

	}

	public static List<String> getIntervalDateStr(String dataType) {
		List<Date> dateList = getIntervalDate(dataType);
		List<String> dateStrList = new ArrayList();
		Calendar calendar = Calendar.getInstance();
		switch (dataType) {
			case "0":
				List<String> tmp=new ArrayList<>();
				tmp.add("00:00");
				tmp.add("02:00");
				tmp.add("04:00");
				tmp.add("06:00");
				tmp.add("08:00");
				tmp.add("10:00");
				tmp.add("12:00");
				tmp.add("14:00");
				tmp.add("16:00");
				tmp.add("18:00");
				tmp.add("20:00");
				tmp.add("22:00");
				tmp.add("23:59");
				return tmp;
			case "1":
			case "2":
				dateStrList=getIntervalTimeList("yyyy-MM-dd",dateList);
				dateStrList.remove(dateStrList.size()-1);
				return dateStrList;
			case "3":
			case "4":
				dateStrList=getIntervalTimeList("yyyy-MM",dateList);
				dateStrList.remove(dateStrList.size()-1);
				return dateStrList;
			default:
				return null;
		}
	}

	/**
	 * 获取时间点
	 *
	 * @param dateFormat
	 * @param dates
	 * @return
	 */
	public static List<String> getIntervalTimeList(String dateFormat, List<Date> dates) {
		List<String> list = new ArrayList<>();
		for (Date date : dates) {
			list.add(convertDate2String(dateFormat, date));
		}
		return list;
	}

	/**
	 * 获取时间点
	 *
	 * @param startDate
	 * @param endDate
	 * @param intervalType
	 * @param interval
	 * @return
	 */
	public static List<Date> getDateIntervalList(Date startDate, Date endDate, int intervalType, int interval) {

		List<Date> list = new ArrayList<>();
		while (startDate.getTime() <= endDate.getTime()) {
			list.add(startDate);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(startDate);
			calendar.add(intervalType, interval);
			if (calendar.getTime().getTime() > endDate.getTime()) {
				if (!startDate.equals(endDate)) {
					list.add(endDate);
				}
				startDate = calendar.getTime();
			} else {
				startDate = calendar.getTime();
			}
		}
		return list;
	}

	public static Date convertString2Date(String format, String dateStr) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		try {
			Date date = simpleDateFormat.parse(dateStr);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String convertDate2String(String format, Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(date);
	}
}
